package com.net.xpay.web.controller.merchant.auto;


import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.AlipayAccountCheckUtil;
import com.net.xpay.alipay.enums.IdentityType;
import com.net.xpay.common.domain.User;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.auto.AutoPayReceiptAccount;
import com.net.xpay.core.manager.auto.AutoPayReceiptAccountManager;
import com.net.xpay.core.model.AutoPayReceiptAccountSearchModel;
import com.net.xpay.core.model.AutoPayReceiptAccountUpdateModel;
import com.net.xpay.core.service.auto.AutoPayReceiptAccountService;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/merchant/auto-pay-receipt-account")
@Api(tags = "商户/自动打款商家自收款账号", description = "商户 自动打款商家自收款账号 net")
public class AutoPayReceiptAccountController {

    @Autowired
    private AutoPayReceiptAccountManager autoPayReceiptAccountManager;

    @Autowired
    private AutoPayReceiptAccountService autoPayReceiptAccountService;

    /**
     * 新增自动打款商家自收款账号
     */
    @ApiOperation("新增自动打款商家自收款账号")
    @PostMapping
    public ResultData<AutoPayReceiptAccount> addAutoPayReceiptAccount(@ApiIgnore @RequestUser User merchant, @RequestBody AutoPayReceiptAccount account) {
        if (!AlipayAccountCheckUtil.isEffectiveAccount(account.getIdentity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号格式不正确");
        }

        if (Strings.isNullOrEmpty(account.getName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "真实姓名不能为空");
        }
        AutoPayReceiptAccountSearchModel searchModel = new AutoPayReceiptAccountSearchModel();
        searchModel.setIdentity(account.getIdentity());
        searchModel.setIsDeleted(0);
        searchModel.setUserId(merchant.getId());
        long count = autoPayReceiptAccountManager.searchCount(searchModel);
        if (count > 0L) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "该支付宝账号已存在，请勿重复添加");
        }

        account.setUserId(merchant.getId());
        account.setReceiptCount(0L);
        account.setIdentityType(IdentityType.ALIPAY_LOGON_ID.getValue());
        account.setIsDeleted(0);

        autoPayReceiptAccountManager.save(account);
        return new ResultData<>(account);
    }

    /**
     * 搜索
     */
    @ApiOperation("搜索")
    @PostMapping("/search")
    public ResultPagedList<AutoPayReceiptAccount> search(@ApiIgnore @RequestUser User merchant, @RequestBody AutoPayReceiptAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        searchModel.setUserId(merchant.getId());
        searchModel.setIsDeleted(0);

        List<AutoPayReceiptAccount> records = autoPayReceiptAccountManager.search(searchModel);
        long count = autoPayReceiptAccountManager.searchCount(searchModel);

        return new ResultPagedList<>(records, count, searchModel);
    }

    /**
     * 修改自动付用户信息
     */
    @ApiOperation("修改自动付用户信息")
    @PostMapping("/update")
    public ResultData<AutoPayReceiptAccount> updateAutoPayAccountInfo(@ApiIgnore @RequestUser User merchant, @RequestBody AutoPayReceiptAccountUpdateModel model) {
        Preconditions.checkArgument(model != null);
        autoPayReceiptAccountService.updateAutoPayAccountInfo(merchant,model);
        return new ResultData<>();
    }

    /**
     * 删除自动付用户信息
     */
    @ApiOperation("删除自动付用户信息")
    @PostMapping("/delete-auto-account")
    public ResultData<AutoPayReceiptAccount> deleteAutoPayAccountInfo(@ApiIgnore @RequestUser User merchant, Long id) {
        Preconditions.checkArgument(id != null);
        autoPayReceiptAccountService.deleteAutoPayAccount(merchant,id);
        return new ResultData<>();
    }


}
